using POC.Common;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using Unity.Transforms;
using UnityEngine;

namespace POC.Client
{
    [WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
    public partial struct InitializeLocalChampSystem : ISystem
    {
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<NetworkId>();
        }

        public void OnUpdate(ref SystemState state)
        {
            var entityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
            //查询到本地自己操控的角色，打上OwnerChampTag组件进行标记，并设置ChampMoveTargetPosition
            foreach (var (transform, entity) in SystemAPI.Query<LocalTransform>().WithAll<GhostOwnerIsLocal>().WithNone<OwnerChampTag>().WithEntityAccess())
            {
                entityCommandBuffer.AddComponent<OwnerChampTag>(entity);
                entityCommandBuffer.SetComponent(entity, new ChampMoveTargetPosition { Value = transform.Position});
            }
            entityCommandBuffer.Playback(state.EntityManager);
        }
    }
}